﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Linq;
using System.Text;
using EntityDataModel;
using DataAccessLayer;
using CommonUtils;

namespace BusinessLogicLayer
{
    public class UserBLL
    {
        /// <summary>     
        /// Get all subscribed users
        /// </summary>
        public static List<User> GetAllUsers() {
            try
            {
                return UserDAL.GetAllUsers();
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Create an User
        /// </summary>
        public static void CreateUser(String userName, String firstName, String lastName, Boolean gender, DateTime? dateOfBirth)
        {
            try
            {
                UserDAL.CreateUser(userName.Trim(), firstName.TrimEnd(), lastName.TrimEnd(), gender, dateOfBirth, DateTime.Now);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }
        public static void CreateUserASP(String userName, String password, string email)
        {
            try
            {
                Membership.CreateUser(userName, password, email);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }
        /// <summary>     
        /// Update an User
        /// </summary>
        public static void UpdateUser(String userName, String firstName, String lastName, Boolean gender, DateTime? dateOfBirth, String email)
        {
            try
            {
                UserDAL.UpdateUser(userName.Trim(), firstName.TrimEnd(), lastName.TrimEnd(), gender, dateOfBirth, email.Trim(), DateTime.Now);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Get user by username
        /// </summary>
        public static User GetUserByUserName(String userName)
        {
            try
            {
                return UserDAL.GetUserByUserName(userName.Trim());
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Get mail for user
        /// </summary>
        public static String GetEmail(String userName)
        {
            try
            {
                return UserDAL.GetEmail(userName.Trim());
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Get all user names
        /// </summary>
        public static String[] GetAllUserNames()
        {
            try
            {
                String[] userNames = UserDAL.GetAllUserNames();
                return userNames;
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Update user's picture
        /// </summary>
        public static void UpdatePictureForUser(String userName, byte[] picture)
        {
            using (liveCPEEntities context = new liveCPEEntities())
            {
                try
                {
                    UserDAL.UpdatePictureForUser(userName, picture);
                }
                catch (Exception e)
                {
                    System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                    LogDAL.AddLog(e.Message, "Error", BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name, DateTime.Now);
                    throw new Exception(sf.GetMethod().Name + " unexpected error", e);
                }
            }
        }

        /// <summary>     
        /// Get user's picture
        /// </summary>
        public static byte[] GetPictureForUser(String userName)
        {
            using (liveCPEEntities context = new liveCPEEntities())
            {
                try
                {
                    return UserDAL.GetPictureForUser(userName);
                }
                catch (Exception e)
                {
                    System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                    LogDAL.AddLog(e.Message, "Error", BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name, DateTime.Now);
                    throw new Exception(sf.GetMethod().Name + " unexpected error", e);
                }
            }
        }

        /// <summary>     
        /// Authentificate user
        /// </summary>
        public static bool DoLogin(String pUserName, String pPassword)
        {
            try
            {
                return Membership.ValidateUser(pUserName, pPassword);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Return true if the given user exists in the table User
        /// </summary>
        public static Boolean IsUser(String userName)
        {
            using (liveCPEEntities context = new liveCPEEntities())
            {
                try
                {
                    return UserDAL.IsUser(userName);
                }
                catch (Exception e)
                {
                    System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                    LogDAL.AddLog(e.Message, "Error", BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name, DateTime.Now);
                    throw new Exception(sf.GetMethod().Name + " unexpected error", e);
                }
            }
        }
    }
}
